package com.wwj.utils;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.wwj.Constant.Constant;
import com.wwj.pojo.API;
import com.wwj.pojo.Case;
import com.wwj.pojo.WriteBackData;
import org.apache.poi.ss.usermodel.*;

import java.io.*;
import java.util.ArrayList;
import java.util.List;


public class ExcelUtils {


    //存储回写数据的list集合
    public static List<WriteBackData> wdbList = new ArrayList<WriteBackData>();


    public static<T> List<T> read(String path,int sheetIndex,Class<T> clazz) {
        System.out.println("-----开始读取excel-----");

        FileInputStream fis = null;
        try {
            //1.加载excel流对象；
            fis = new FileInputStream(path);
            //2.导入参数对象,默认读取第一个sheet,表头是sheet的第一行
            ImportParams params = new ImportParams();
            //3、工具类解析excel封装成list对象
            params.setNeedVerfiy(true);
            params.setStartSheetIndex(sheetIndex);
            //Class clazz = API.class

            List<T> list = ExcelImportUtil.importExcel(fis, clazz, params);
            System.out.println(list);
            return list;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            close(fis);
        }
        return null;


    }


    public static Object[][] read1(String path){
        FileInputStream fis = null;
        Object[][] datas = null;  //需要传给用例的数据如何和excel结合

        try {
            //1.找到excel文件，
            fis = new FileInputStream(path);
            //2.打开workbook对象就是excel,兼容2003xls和2007xlsx
            //多态
            Workbook workbook = WorkbookFactory.create(fis);

            //3.获取sheet
            Sheet sheet = workbook.getSheet("用例");
            //4.获取行
            //4.1获取最大行号
            int lastRowNum = sheet.getLastRowNum();
            //定义datas数组，进行初始化
            //datas = new Object[接口执行次数（行数）][接口参数个数（列）];
            datas = new Object[lastRowNum][4];

            //4.2遍历所有行
            for (int i =1;i<=lastRowNum;i++){
                //4.3获取当前行
              Row row = sheet.getRow(i);
                /*------ caseId---------*/
                Cell caseIdCell = row.getCell(0, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                caseIdCell.setCellType(CellType.STRING);
                //获取caseid单元格的值
                String caseIdCellValue =  caseIdCell.getStringCellValue();

                /*------ url---------*/
                Cell urlCell = row.getCell(1, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                urlCell.setCellType(CellType.STRING);
                //获取url的值
                String urlCellValue = urlCell.getStringCellValue();
                datas[i-1][0]=urlCellValue;



                /*------ type---------*/
                Cell typeCell = row.getCell(2, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                typeCell.setCellType(CellType.STRING);
                //获取url的值
                String typeCellValue = typeCell.getStringCellValue();
                datas[i-1][1]=typeCellValue;


                /*------ Content-Type---------*/
                Cell ContenttypeCell = row.getCell(3, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                ContenttypeCell.setCellType(CellType.STRING);
                //获取url的值
                String ContenttypeCellValue = ContenttypeCell.getStringCellValue();
                datas[i-1][2]=ContenttypeCellValue;

                /*------ params---------*/

                Cell paramscell = row.getCell(4, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                paramscell.setCellType(CellType.STRING);
                String paramsCellValue = paramscell.getStringCellValue();
                datas[i-1][3]=paramsCellValue;

                System.out.println(caseIdCellValue+","+urlCellValue+","+typeCellValue+","+paramsCellValue+","+ContenttypeCellValue);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //关闭资源流，一定会执行
           close(fis);
        }

        return datas;
    }

    /**关闭输入流和输出流
     * @param stream
     */
//写出一个统一的可以关闭输出流和输入流
    private static void close(Closeable stream) {
        try {
            if(stream!=null){
                stream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**回写数据返回结果到excel文件
     * @param path
     * @param sheetIndex
     */

    public static void write(String path, int sheetIndex){
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            //1.找到excel文件
            fis = new FileInputStream(path);
            //2.打开excel文件
            Workbook workbook = WorkbookFactory.create(fis);
            //3.获取sheet
            Sheet sheet = workbook.getSheetAt(sheetIndex);
            //4.获取要写的行和列定位到单元格
            //循环wbdlist集合，取出wbd对象获取到行号，列号，内容
            for(WriteBackData wbd:wdbList) {
                Row row = sheet.getRow(wbd.getRowNum());
                //4.2获取第三列
                Cell cell = row.getCell(wbd.getCellNum(), Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                cell.setCellType(CellType.STRING);

                //5.修改内容
                cell.setCellValue(wbd.getContent());
            }

            //目前所有操作都在java里面
            fos = new FileOutputStream(path);
            workbook.write(fos);


        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //6.关闭流
            close(fis);
            close(fos);
        }


    }


}
